package org.dromara.bclt.service.impl;

import org.dromara.bclt.domain.DeviceInfo;
import org.dromara.bclt.domain.vo.DeviceInfoVo;
import org.dromara.bclt.mapper.DeviceInfoMapper;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bclt.domain.bo.CourseStatusBo;
import org.dromara.bclt.domain.vo.CourseStatusVo;
import org.dromara.bclt.domain.CourseStatus;
import org.dromara.bclt.mapper.CourseStatusMapper;
import org.dromara.bclt.service.ICourseStatusService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 视频播放状态Service业务层处理
 *
 * @author RRyuan
 * @date 2024-10-23
 */
@RequiredArgsConstructor
@Service
public class CourseStatusServiceImpl implements ICourseStatusService {

    private final CourseStatusMapper baseMapper;
    private final DeviceInfoMapper deviceInfoMapper;

    /**
     * 查询视频播放状态
     *
     * @param id 主键
     * @return 视频播放状态
     */
    @Override
    public CourseStatusVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询视频播放状态列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 视频播放状态分页列表
     */
    @Override
    public TableDataInfo<CourseStatusVo> queryPageList(CourseStatusBo bo, PageQuery pageQuery) {
        if(bo.getDeviceNumber() != null){
            DeviceInfoVo deviceInfoVo = deviceInfoMapper.selectVoOne(new LambdaQueryWrapper<DeviceInfo>()
                .select(DeviceInfo::getId)
                .eq(DeviceInfo::getDeviceNumber, bo.getDeviceNumber()));
            Long deviceId = null;
            if(deviceInfoVo != null){
                deviceId = deviceInfoVo.getId();
            }
            bo.setDeviceId(deviceId);
        }
        LambdaQueryWrapper<CourseStatus> lqw = buildQueryWrapper(bo);
        Page<CourseStatusVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的视频播放状态列表
     *
     * @param bo 查询条件
     * @return 视频播放状态列表
     */
    @Override
    public List<CourseStatusVo> queryList(CourseStatusBo bo) {
        LambdaQueryWrapper<CourseStatus> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CourseStatus> buildQueryWrapper(CourseStatusBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CourseStatus> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, CourseStatus::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getCourseId() != null, CourseStatus::getCourseId, bo.getCourseId());
        lqw.eq(StringUtils.isNotBlank(bo.getCourseStatus()), CourseStatus::getCourseStatus, bo.getCourseStatus());
        lqw.eq(bo.getFinishTime() != null, CourseStatus::getFinishTime, bo.getFinishTime());
        lqw.eq(bo.getPlayProgress() != null, CourseStatus::getPlayProgress, bo.getPlayProgress());
        lqw.eq(bo.getCreateTime() != null, CourseStatus::getCreateTime, bo.getCreateTime());
        lqw.eq(bo.getCourseViewingDuration() != null, CourseStatus::getCourseViewingDuration, bo.getCourseViewingDuration());
        return lqw;
    }

    /**
     * 新增视频播放状态
     *
     * @param bo 视频播放状态
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CourseStatusBo bo) {
        //根据deviceNumber查询deviceId
        if(bo.getDeviceNumber() != null) {
            DeviceInfoVo deviceInfoVo = deviceInfoMapper.selectVoOne(new LambdaQueryWrapper<DeviceInfo>()
                .select(DeviceInfo::getId)
                .eq(DeviceInfo::getDeviceNumber, bo.getDeviceNumber()));
            Long deviceId = null;
            if(deviceInfoVo != null) {
                deviceId = deviceInfoVo.getId();
            }
            bo.setDeviceId(deviceId);
        }

        // 检查记录是否已经存在
        CourseStatus existingRecord = baseMapper.selectOne(new LambdaQueryWrapper<CourseStatus>()
            .eq(CourseStatus::getDeviceId, bo.getDeviceId())
            .eq(CourseStatus::getCourseId, bo.getCourseId()));
        if (existingRecord == null){
            CourseStatus add = MapstructUtils.convert(bo, CourseStatus.class);
            boolean flag = baseMapper.insert(add) > 0;
            validEntityBeforeSave(add);
            if (flag) {
                bo.setId(add.getId());
            }
            return flag;
        }else {
            bo.setId(existingRecord.getId());
            return updateByBo(bo);
        }
    }

    /**
     * 修改视频播放状态
     *
     * @param bo 视频播放状态
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CourseStatusBo bo) {
        CourseStatus update = MapstructUtils.convert(bo, CourseStatus.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CourseStatus entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除视频播放状态信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
